package com.android.love.web;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class EventController {

	private List<IDownloadEventsListener> mDownloadListeners;
	private static final EventController INSTANCE = new EventController();

	public static EventController getInstance() {
		return INSTANCE;
	}

	private EventController() {
		mDownloadListeners = new ArrayList<IDownloadEventsListener>();
	}

	/**
	 * 添加一个下载事件监听器
	 * 
	 * @param listener
	 */
	public synchronized void addDownloadListener(
			IDownloadEventsListener listener) {

		if (!mDownloadListeners.contains(listener)) {
			mDownloadListeners.add(listener);
		}
	}

	/**
	 * 移除一个下载监听器
	 * 
	 * @param listener
	 */
	public synchronized void removeDownloadListener(
			IDownloadEventsListener listener) {
		mDownloadListeners.remove(listener);
	}

	/**
	 * 触发一个下载事件
	 * 
	 * @param event
	 *            The event.
	 * @param data
	 *            Additional data.
	 */
	public synchronized void fireDownloadEvent(String event, Object data) {
		Iterator<IDownloadEventsListener> iter = mDownloadListeners.iterator();
		while (iter.hasNext()) {
			iter.next().onDownloadEvent(event, data);
		}
	}

}
